package servlet.Student;

import bean.Student;
import dao.impl.StudentIDaoImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/student/studentadd")
public class StudentAdd extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        String sno = req.getParameter("sno");
        String sname = req.getParameter("sname");
        String ssex = req.getParameter("ssex");
        String sage = req.getParameter("sage");
        String sdept = req.getParameter("sdept");
        Student student = new Student();
        student.setSno(sno);
        student.setSname(sname);
        student.setSsex(ssex);
        student.setSage(sage);
        student.setSdept(sdept);
        StudentIDaoImp studentIDaoImp = new StudentIDaoImp();
        int row = studentIDaoImp.addStudent(student);
        HttpSession session = req.getSession();
        if (row > 0) {
            req.getRequestDispatcher("/studentAllServlet").forward(req,resp);
        }else{
            session.setAttribute("msg","内部错误，稍后重试！"); //存储到缓存
            resp.sendRedirect("/err/error.jsp");
        }
    }
}
